package com.example.teesystem.entityDto;

import com.example.teesystem.common.cache.KnowledgeCache;
import com.example.teesystem.common.utils.FileUtil;
import com.example.teesystem.entity.Issue;
import com.example.teesystem.entity.Knowledge;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : NieYaShi
 * @version : v1.0
 * @createTime : 2022/4/12 14:01
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class IssueSimpleMessage {
    private Integer id;

    private String question;

    private String selectionA;

    private String selectionB;

    private String selectionC;

    private String selectionD;

    private String answer;

    private String issueType;

    private List<String> urlList = new ArrayList<>();

    private Integer type;

    private List<Knowledge> knowledgeList = new ArrayList<>();

    public IssueSimpleMessage(Issue issue){
        BeanUtils.copyProperties(issue, this);

        String url = issue.getUrl().trim();
        if (!url.equals("")){
            String[] urls = url.split("\\$");
            for (String fileUrl : urls){
                urlList.add(FileUtil.FILE_PREFIX + fileUrl);
            }
        }

        if (issue.getKnowledgeList() != null && !issue.getKnowledgeList().equals("")){
            String[] knowledgeIds = issue.getKnowledgeList().split("\\$");
            Knowledge def = new Knowledge();
            def.setMessage("");
            for (String knowledgeId : knowledgeIds) {
                Knowledge knowledge = KnowledgeCache.knowledgeMap.getOrDefault(Integer.parseInt(knowledgeId), def);
                knowledgeList.add(knowledge);
            }
        }
    }
}
